<?php

namespace app\admin\controller\statistics;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\Env;

/**
 * 诊疗服务明细
 *
 * @icon fa fa-user
 */
class Serve extends Backend
{


    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $models = null;
    protected $categorylist = [];
    private $redis;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;
        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                //->with(["info"=>['id','name','user_id'],"medic"=>['id','name']])
                ->with([
                    'info' => function ($query) {
                        $query->withField('id,name,user_id');
                    },
                    'medic' => function ($query) {
                        $query->withField('id,name');
                    },
                ])
                ->where($where)
                ->order('id', 'desc')
                ->paginate($limit);

            if (!empty($list->items())){
                foreach ($list->items() as $k=>$v){
                    $count = Db::table('fa_treatment')
                        ->where('order_id',$v['id'])
                        ->count();
                    $list->items()[$k]->counts = '共'.$count.'次';
                }
            }

            $output = $this->model
                ->with(["info"=>['id','name','user_id'],"medic"=>['id','name']])
                ->where($where)
                ->where('order.status','in',[1,2])
                ->sum('reality_cost');

            $result = array("total" => $list->total(), "rows" => $list->items(),"extend" => ['money' => $output]);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);

        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $info = Db::table('fa_info')
            ->alias('a')
            ->field('a.*,b.level,b.vip')
            ->join('fa_users b','a.user_id=b.id','left')
            ->where('a.id',$row->info_id)
            ->find();

        //诊疗单
        $diagnosiss = Db::table('fa_diagnosiss')
            ->where('order_id',$row->id)
            ->select();

        //药品单
        $drug = Db::table('fa_drug')
            ->where('order_id',$row->id)
            ->select();

        //会员服务单
        $vip = Db::table('fa_serve')
            ->where('order_id',$row->id)
            ->select();

        //体检项目
        $health = Db::table('fa_health_log')
            ->where('order_id',$row->id)
            ->select();

        //疗程数据
        $treatment = Db::table('fa_treatment')
            ->where('order_id',$row->id)
            ->select();

        if (!empty($info)){
            $info['age'] = countage($info['birthday']);
            $level_name = Db::table('fa_vip_level')
                ->where('level',$info['level'])
                ->find();
            if (!empty($level_name)){
                $info['level_name'] = $level_name['level_name'];
            }else{
                $info['level_name'] = '';
            }

            $card_arr = Db::table('fa_vip_card')
                ->where('id',$info['vip'])
                ->find();

            if (!empty($card_arr)){
                $info['level_name'] = $level_name['level_name'].'('.$card_arr['name'].')';
            }

        }


        //1代表转账，2代表现金，3代表pos机，4代表支付宝，5代表微信，6代表储值卡，7代表积分
        switch ($row->pay){
            case 1:
                $row->pay = '转账';
                break;
            case 2:
                $row->pay = '现金';
                break;
            case 3:
                $row->pay = 'pos机';
                break;
            case 4:
                $row->pay = '支付宝';
                break;
            case 5:
                $row->pay = '微信';
                break;
            case 6:
                $row->pay = '储值卡';
                break;
            case 7:
                $row->pay = '积分';
                break;
            default:
                $row->pay = '无';
                break;
        }

        $row->receivable = number_format($row->count_cost+$row->other_cost-$row->vip_discounts-$row->iscounts_cost-$row->vip_deduction+($row->new_price),2,'.','');

        if ($row->on_credit == 2){
            $row->on_account = '0.00';
        }

        $row->is_custom = 0;
        if (!empty($row->new_str)){
            $row->new_str = $row->new_str.':'.$row->new_price;
            $row->is_custom = 1;
        }

        $this->view->assign("row", $row);
        $this->view->assign("info", $info);
        $this->view->assign("diagnosiss", $diagnosiss);
        $this->view->assign("drug", $drug);
        $this->view->assign("vip", $vip);
        $this->view->assign("health", $health);
        $this->view->assign("treatment", $treatment);
        return $this->view->fetch();
    }


}
